package com.toycloud.business.controller.business;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.toycloud.business.dto.params.FeedbackAnalysisParams;
import com.toycloud.business.dto.params.business.FeedbackCompareParams;
import com.toycloud.business.dto.params.business.FeedbackProductParamManagerParams;
import com.toycloud.business.dto.results.CommonResult;
import com.toycloud.common.core.controller.BaseController;
import com.toycloud.common.core.utils.poi.ExcelUtil;
import com.toycloud.common.core.web.domain.AjaxResult;
import com.toycloud.system.domain.business.FeedbackProductParamManager;
import com.toycloud.system.domain.business.ParamImportVO;
import com.toycloud.system.log.annotation.Log;
import com.toycloud.system.log.enums.BusinessType;
import com.toycloud.system.mapper.business.FeedbackProductParamManagerMapper;
import com.toycloud.system.mapper.business.FeedbackProductParamScoreMapper;
import com.toycloud.system.service.business.IFeedbackProductParamManagerDetailService;
import com.toycloud.system.service.business.IFeedbackProductParamManagerService;
import com.toycloud.system.utils.Openstorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 产品参数管理Controller
 * 
 * @author ruoyi
 * @date 2024-12-18
 */
@RestController
@RequestMapping("/parammanager")
public class FeedbackProductParamManagerController extends BaseController
{
    @Autowired
    private IFeedbackProductParamManagerService feedbackProductParamManagerService;
    @Resource
    private FeedbackProductParamManagerMapper feedbackProductParamManagerMapper;
    @Autowired
    private IFeedbackProductParamManagerDetailService feedbackProductParamManagerDetailService;
    @Resource
    private FeedbackProductParamScoreMapper feedbackProductParamScoreMapper;
    @Autowired
    private Openstorage openstorage;

    /**
     * 查询产品参数管理列表
     */
    @GetMapping("/page")
    public CommonResult<List<FeedbackProductParamManager>> page(FeedbackProductParamManagerParams feedbackProductParamManagerParams)
    {
        return feedbackProductParamManagerService.selectFeedbackProductParamManagerList(feedbackProductParamManagerParams);
    }

    /**
     * 导出产品参数管理列表
     */
    @Log(title = "产品参数管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FeedbackProductParamManagerParams feedbackProductParamManagerParams)
    {
        List<FeedbackProductParamManager> list = feedbackProductParamManagerMapper.selectFeedbackProductParamManagerList(feedbackProductParamManagerParams);
        ExcelUtil<FeedbackProductParamManager> util = new ExcelUtil<FeedbackProductParamManager>(FeedbackProductParamManager.class);
        util.exportExcel(response, list, "产品参数管理数据");
    }

    @GetMapping("/tableTitle")
    public AjaxResult tableTitle(){
        return AjaxResult.success(feedbackProductParamManagerMapper.tableTitle());
    }

    @GetMapping("/tableParam")
    public AjaxResult tableParam(){
        return AjaxResult.success(feedbackProductParamManagerService.tableParam());
    }
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@Valid @RequestBody FeedbackProductParamManager feedbackProductParamManager) throws Exception {
        return feedbackProductParamManagerService.insertFeedbackProductParamManager(feedbackProductParamManager);
    }
    @PostMapping("/update")
    @Transactional
    public AjaxResult update(@Valid @RequestBody FeedbackProductParamManager feedbackProductParamManager)
    {
        return feedbackProductParamManagerService.updateFeedbackProductParamManager(feedbackProductParamManager);
    }

    @PostMapping("/export2")
    public CommonResult<String> export2(@RequestBody FeedbackProductParamManagerParams params) {
        return feedbackProductParamManagerService.export2(params);
    }

    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable String id)
    {
        feedbackProductParamManagerService.deleteFeedbackProductParamManagerById(id);
        feedbackProductParamManagerDetailService.deleteFeedbackProductParamManagerDetailByParentid(id);
        feedbackProductParamScoreMapper.deleteFeedbackProductParamScoreByParentid(id);
        return AjaxResult.success("删除成功");
    }
    @GetMapping("/{id}")
    public AjaxResult getDemandRecord(@PathVariable String id)
    {
        return AjaxResult.success(feedbackProductParamManagerService.selectFeedbackProductParamManagerById(id));
    }
    @PostMapping("/importTemp")
    public void importTemp(HttpServletResponse response)
    {
        List<ParamImportVO> list = feedbackProductParamManagerService.importTemp();
        ExcelUtil<ParamImportVO> util = new ExcelUtil<ParamImportVO>(ParamImportVO.class);
        util.exportExcel(response, list, "产品参数管理数据");
    }
    @PostMapping("/importData")
    public AjaxResult importTemp(@RequestBody FeedbackProductParamManager feedbackProductParamManager)
    {
        return feedbackProductParamManagerService.importData(feedbackProductParamManager);
    }
/**
 * @Auther: doubaolin
 * @Date: 2024/12/25 16:59
 * @Description: 竞品分析查询下拉,暂时没用级联下拉
 * @param
 * @return:
 */
    @GetMapping("/compareSelectList")
    public AjaxResult compareSelectList(@RequestParam(value = "id", required = false) String id,@RequestParam(value = "type") String type)
    {
        return AjaxResult.success(feedbackProductParamManagerService.compareSelectList(type,id));
    }

    /**
     * @Auther: doubaolin
     * @Date: 2024/12/26 9:50
     * @Description: 竞品分析页面1
     * @param
     * @return:
     */
    @PostMapping("/compareQueryList")
    public AjaxResult compareQueryList( @RequestBody FeedbackCompareParams params)
    {
        if(params.getProductbrandid()==null){
            return AjaxResult.error("产品品牌必选");
        }
        if(params.getCompareProducts()!=null && params.getCompareProducts().size()!= 0){
            params.setCompareProducts(params.getCompareProducts().stream().filter(compareProducts -> compareProducts!=null).collect(Collectors.toList()));
            for (int i = 0; i < params.getCompareProducts().size(); i++) {
                if(params.getCompareProducts().get(i).getProductbrandid()==null){
                    return AjaxResult.error("产品品牌必选");
                }
            }
        }

        return feedbackProductParamManagerService.compareQueryList(params);
    }
    /**
     * @Auther: doubaolin
     * @Date: 2024/12/26 9:50
     * @Description: 竞品分析页面2
     * @param
     * @return:
     */
    @PostMapping("/compareQueryListDetail")
    public AjaxResult compareQueryListDetail( @RequestBody FeedbackCompareParams params)
    {
        if(params.getProductbrandid()==null){
            return AjaxResult.error("产品品牌必选");
        }
        if(params.getCompareProducts()!=null && params.getCompareProducts().size()!= 0){
            params.setCompareProducts(params.getCompareProducts().stream().filter(compareProducts -> compareProducts!=null).collect(Collectors.toList()));
            for (int i = 0; i < params.getCompareProducts().size(); i++) {
                if(params.getCompareProducts().get(i).getProductbrandid()==null){
                    return AjaxResult.error("产品品牌必选");
                }
            }
        }
        return feedbackProductParamManagerService.compareQueryListDetail(params);
    }
    @PostMapping("/compareQueryListDetailTable")
    public AjaxResult compareQueryListDetailTable( @RequestBody FeedbackCompareParams params)
    {
        if(params.getProductbrandid()==null){
            return AjaxResult.error("产品品牌必选");
        }
        if(params.getCompareProducts()!=null && params.getCompareProducts().size()!= 0){
            params.setCompareProducts(params.getCompareProducts().stream().filter(compareProducts -> compareProducts!=null).collect(Collectors.toList()));
            for (int i = 0; i < params.getCompareProducts().size(); i++) {
                if(params.getCompareProducts().get(i).getProductbrandid()==null){
                    return AjaxResult.error("产品品牌必选");
                }
            }
        }
        return feedbackProductParamManagerService.compareQueryListDetailTable(params);
    }

    @GetMapping("/selectParamOption")
    public AjaxResult paramList(){
        return feedbackProductParamManagerService.paramList();
    }

    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(MultipartFile multipartFile){
        try{
            String url = null;
            url = openstorage.uploadFileNew(multipartFile.getInputStream(),
                    "talkres", "feedback", multipartFile.getOriginalFilename());
            return AjaxResult.success("成功",url);
        }catch (IOException e){
            return AjaxResult.error("上传失败");
        }
    }
}
